package ui.fragment;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.PassengerDetailBean;
import com.fy.fyzf.bean.PassengerListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SearchPassengerBean;
import com.fy.fyzf.listener.PermissionListener;
import com.fy.fyzf.presenter.PassengerPresnter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.view.PassengerCallBack;
import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import ui.activity.EditDemandActivity;
import ui.activity.MyPassengerSourceActivity;
import ui.adapter.PassengerSourceAdapter;

/**
 * 客源
 * A simple {@link Fragment} subclass.
 * Use the {@link PassengerSourceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PassengerSourceFragment extends BaseFragment<PassengerPresnter> implements PassengerCallBack {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.et_search)
    EditText edSearch;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private PassengerSourceAdapter passengerSourceAdapter;

    public PassengerSourceFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PassengerSourceFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PassengerSourceFragment newInstance(String param1, String param2) {
        PassengerSourceFragment fragment = new PassengerSourceFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    private int tag = 1;
    private int pageNumber = 1;
    private int pageSize = 10;
    @Override
    protected PassengerPresnter createPresenter() {
        return new PassengerPresnter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_passenger_source;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unregisterEventBus(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvenet(String s){
        if (s.equals("editPassengerSuccess")) {
            loadData();
        }
    }
    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        registerEventBus(this);
        passengerSourceAdapter = new PassengerSourceAdapter(getActivity());
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycleView.setAdapter(passengerSourceAdapter);

        passengerSourceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<PassengerListBean.ListBean> data = adapter.getData();
                Intent intent = new Intent(getActivity(), MyPassengerSourceActivity.class);
                intent.putExtra("id", data.get(position).getCustomerId());
                startActivity(intent);
            }
        });

        passengerSourceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<PassengerListBean.ListBean> data = adapter.getData();
                if (view.getId() == R.id.iv_call) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.get(position).getCustomerMobile()));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }

            }
        });
        edSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                SearchPassengerBean searchPassengerBean = new SearchPassengerBean();
                pageNumber = 1;
                tag = 1;
                searchPassengerBean.setPageNum(pageNumber);
                searchPassengerBean.setPageSize(pageSize);
                if (TextUtils.isEmpty(s)) {
                    searchPassengerBean.setCustomerName(null);
                } else {
                    searchPassengerBean.setCustomerName(s.toString());
                }
                mPresenter.getPassengerList(searchPassengerBean);
            }
        });
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNumber++;
                tag = 2;
                loadData();
            }
        });
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNumber = 1;
                tag = 1;
                loadData();
            }
        });

        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), EditDemandActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        SearchPassengerBean searchPassengerBean = new SearchPassengerBean();
        searchPassengerBean.setPageNum(pageNumber);
        searchPassengerBean.setPageSize(pageSize);
        Log.e("客源数据", new Gson().toJson(searchPassengerBean));
        mPresenter.getPassengerList(searchPassengerBean);
    }

    @Override
    public void getPassengerListSuccess(PassengerListBean passengerListBean) {
        Log.e("客源", new Gson().toJson(passengerListBean));
        smartRefresh.finishLoadMore();
        smartRefresh.finishRefresh();
        if (passengerListBean.getList().size() <= 0 || null == passengerListBean) {
            passengerSourceAdapter.setEmptyView(R.layout.layout_nodata_common, recycleView);
        }
        if (tag == 1) {
            PassengerListBean.ListBean listBean = passengerListBean.getList().get(0);
            passengerSourceAdapter.setNewData(passengerListBean.getList());
        } else {
            passengerSourceAdapter.addData(passengerListBean.getList());
        }
    }

    @Override
    public void getPassengerDetailSuccess(PassengerDetailBean passengerDetailBean) {

    }

    @Override
    public void getCollectHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void getMatchHousingSuccess(HousesListBean housesListBean) {



    }

    @Override
    public void getRecommendHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void AddPassengerSourceSuccess(BaseData baseData) {

    }

    @Override
    public void getSiteListSuccess(List<AreaListBean> areaListBeans) {

    }

    @Override
    public void editPassengerSuccess(BaseData baseData) {

    }
}